package manage.actions;

import manage.commons.Constants;
import manage.commons.annontations.RequiredLogin;
import manage.commons.annontations.RequiredLogin.Required;
import manage.commons.cookie.UserState;
import manage.commons.cookie.UserStateSessionManager;
import manage.commons.utils.CookieUtils;
import manage.commons.utils.EncryptUtils;
import manage.commons.utils.RandomCode;
import manage.modules.worker.model.Worker;
import manage.modules.worker.service.WorkerService;
import manage.modules.worker.util.WorkerLoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

@RequiredLogin(Required.FALSE)
public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 1454290375051949551L;
	private String forward;
	private Long workerId;
	private String password;
	private String captcha;
	private WorkerService workerService;
	private boolean showCaptcha;

	@Override
	public void validate() {
		if (null == getSession().getAttribute(
				Constants.SESSION_CURRENT_WORKER_ID)) {
			if (workerId == null) {
				addFieldError("workerId", "请填写工号");
			}
			if (StringUtils.isEmpty(password)) {
				addFieldError("password", "请填写登录密码");
			}
			if (showCaptcha) {
				if (StringUtils.isEmpty(captcha)) {
					addFieldError("captcha", "请填写验证码");
				}
			}
		}
	}

	@Override
	@Action(value = "login-input", results = { @Result(name = SUCCESS, location = Constants.LOGIN_PAGE) })
	public String input() throws Exception {
		return SUCCESS;
	}

	@Override
	@Action(results = { @Result(name = SUCCESS, type = "redirect", location = "${forward}") })
	public String execute() throws Exception {
		if (showCaptcha) {
			String captchaCookieValue = CookieUtils.getCookie(request,
					Constants.COOKIE_CAPTCHA_KEY);
			if (!StringUtils.equals(captchaCookieValue,
					RandomCode.getEncryptValue(captcha))) {
				addFieldError("captchaError", "验证码输入错误，请重新输入");
				return INPUT;
			}
		}
		String cookieUser = CookieUtils.getCookie(request, Constants.COOKIE_U);
		final UserStateSessionManager sessionManager = new UserStateSessionManager(
				cookieUser);
		UserState session = sessionManager.getUserFormCookie(cookieUser);
		if (null == session) {
			boolean existsWorker = workerService.exists(workerId);
			if (existsWorker) {
				Worker loginWorker = workerService.select(workerId);
				String encryPasswd = EncryptUtils.encryptPwd(password);
				if (!StringUtils.equals(encryPasswd, loginWorker.getPassword())) {
					addFieldError("password", "密码不正确");
					return INPUT;
				}
				WorkerLoginUtil.login(workerService.selectWorkerInfo(workerId),
						request, response);
			} else {
				addActionError("你要登录的工号并不存在");
				return INPUT;
			}
		}
		// 如果没有访问某些路径则默认跳到首页
		if (StringUtils.isBlank(forward)) {
			forward = Constants.INDEX_ACTION;
		}
		return SUCCESS;
	}

	public String getForward() {
		return forward;
	}

	public void setForward(String forward) {
		this.forward = forward;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Long getWorkerId() {
		return workerId;
	}

	public void setWorkerId(Long workerId) {
		this.workerId = workerId;
	}

	public void setWorkerService(WorkerService workerService) {
		this.workerService = workerService;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public String getCaptcha() {
		return captcha;
	}

	public boolean getShowCaptcha() {
		return showCaptcha;
	}

	public void setShowCaptcha(boolean showCaptcha) {
		this.showCaptcha = showCaptcha;
	}
}
